#!/bin/bash
# - source
# - repo_name

# source is online repository or local repository, local repository
# should use absolute path

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/upload.sh

[ -n "$source" ]	|| die "source is empty"
[ -n "$repo_name" ]	|| die "repo_name is empty"

: "${os:=openeuler}"
: "${os_version:=20.03-LTS-SP1}"

dest_dir=/rpm/upload/${os}-${os_version}

check_source()
{
	source=($(echo ${source}))

	if [[ ${source,,} =~ ^https?:// ]]; then
		is_url
	else
		is_local
	fi
}

mirror_repo()
{
	local LFTP_TIMEOUT='set net:timeout 2; set net:reconnect-interval-base 2; set net:max-retries 2;'
	local exclusions="-x repocache"

	for i in ${!source[@]}
	do
		dir_name=$(echo ${source[i]} | sed 's;/$;;g')
		dir_name=${dir_name##*/}
		if [ ${#source[@]} == "1" ]; then
			paras="$LFTP_TIMEOUT; open ${source[i]}; lcd /tmp/repo/${repo_name}; mirror -aen -P 10 ${exclusions}"
			mkdir -p /tmp/repo/${repo_name} && lftp -c "${paras}"
		else
			paras="$LFTP_TIMEOUT; open ${source[i]}; lcd /tmp/repo/${i}; mirror -aen -P 10 ${exclusions}"
			mkdir -p /tmp/repo/${i} && lftp -c "${paras}"
		fi
	done

	[ ${#source[@]} == "1" ] || combine_repo
}

detect_arch()
{
	arches=($(show_local_files /tmp/repo/ | grep rpm$ | grep -v src.rpm | awk -F '.' '{print $(NF-1)}' | sort -u))

	[[ ${arches[*]} =~ 'noarch' ]] && {
		arches=($(echo ${arches[*]} | sed 's;noarch ;;g'))
		has_noarch="true"
	}
}

move_file()
{
	local src_dir=$1
	local arch=$2

	for srpm in $(show_local_files ${src_dir} | grep src.rpm$ | grep ${arch})
	do
		mv ${srpm} ${target_dir}/standard_${arch}/src/Packages
	done

	for rpm in $(show_local_files ${src_dir} | grep ${arch} | grep ${arch}.rpm$)
	do
		mv ${rpm} ${target_dir}/standard_${arch}/${arch}/Packages
	done

	[ ${has_noarch} == "true" ] && {
		for rpm in $(show_local_files ${src_dir} | grep ${arch} | grep noarch | grep noarch.rpm$)
		do
			mv ${rpm} ${target_dir}/standard_${arch}/${arch}/Packages
		done
	}
}

combine_repo()
{
	target_dir="/tmp/repo/${repo_name}"

	detect_arch

	for arch in ${arches[@]}
	do
		mkdir -p ${target_dir}/standard_${arch}/{${arch}/Packages,src/Packages}

		for src_dir in $(ls -d /tmp/repo/* | grep -v ${repo_name})
		do
			move_file ${src_dir} ${arch}
		done
	done
}

show_local_files()
{
	find $1 -type f
}

deal_upload_dir()
{
	local file=$1
	local string=$2

	dir="${file//${string}/}"
	dir="${dir%/*}"
}

upload_files()
{
	for file in $(show_local_files /tmp/repo/${repo_name})
	do
		deal_upload_dir ${file} '/tmp/repo'

		upload_one_curl ${file} ${dest_dir}/${dir}
	done
}

is_repo()
{
	echo find | lftp ${source} | grep -qF repodata
}

is_url()
{
	local HTTP_CODE=$(curl -o /dev/null -s --head -w "%{http_code}" "${source}")

	if [ ${HTTP_CODE} -eq "200" ]; then
		is_repo || die "${url} is not repository"
		mirror_repo
		upload_files
	else
		die "${source} unreachable"
	fi
}

is_local()
{
	if  [ -d "${source}" ]; then
		for file in $(show_local_files ${source})
		do
			deal_upload_dir ${file} ${source}

			upload_one_curl ${file} ${dest_dir}/${dir}
		done
	else
		die "${source} is not directory"
	fi
}

check_source
